%no : nomor soal berdasarkan soal tugas
%A  : matriks data
function [hasil] = mn_newton(A, titik_interpolasi, derajat, handles)
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here
if (length(A(1,:))==2)          %interpolasi 1 peubah
    X = A(:,1);
    Y = A(:,2);
    disp(X);
    disp(Y);
    
    N = length(X);
    ST = zeros(N, N);
    if(derajat > N) 
        derajat = N;
    end
    
    % simpan f(x) pada kolom pertama ST
    ST(:,1) = Y';
    
    % buat tabel ST
    for k = 2 : N + 1
        for i = 1 : N - k + 1
            ST(i, k) = (ST(i + 1, k - 1) - ST(i, k - 1))/(X(i + k - 1) - X(i));
        end
    end
    
    set(handles.uitable_selisih,'Data',ST);       
    
    %menggambar polinom interpolasi
    for xx=A(1,1):A(length(A(:,1)),1)
        jumlah = ST(1,1);
        for k = 2 : derajat
            suku = ST(1, k);
            for i = 1 : k - 1
                suku = suku * (xx - X(i));
            end
            jumlah = jumlah + suku;
        end
        P(xx-A(1,1)+1,1) = xx;
        P(xx-A(1,1)+1,2) = jumlah;
    end
    axis(handles.axes1);
    cla(handles.axes1,'reset');
    plot(P(:,1),P(:,2),'b');
    hold on;
    plot(A(:,1),A(:,2),'r*');
    
    jumlah = ST(1,1);
    for k = 2 : derajat
        suku = ST(1, k);
        for i = 1 : k - 1
            suku = suku * (titik_interpolasi - X(i));
        end
        jumlah = jumlah + suku;
    end

    hasil = jumlah;
    hasil
    
elseif (length(A(1,:))==3)      %interpolasi 2 peubah
    titik_interpolasi = [15 50];
    X = A(:,1);
    Y = A(:,2);
    Z = A(:,3);
    D = length(X);
    XL = X(1);
    Table1 = 0;
    Table2 = 0;
    k = 1;
    l = 1;
    for i = 1:D
        if (XL ~= X(i))
            Table1
            Table2(k,1)= XL;
            Table2(k,2)= mn_newton(Table1,titik_interpolasi(1,2),derajat, handles);
            XL = X(i);
            l = 1;
            k = k +1;
            Table1 = 0;
        end
        Table1(l,1) = Y(i);
        Table1(l,2) = Z(i);
        l = l + 1;
    end
    Table2
    Table2(k,1)= XL;
    Table2(k,2)= mn_newton(Table1,titik_interpolasi(1,2),derajat, handles);
    Table2;
    hasil = mn_newton(Table2,titik_interpolasi(1,1),derajat, handles);
    
end

